

#if defined(__LINUX__)

#include <pthread.h>
#include <sys/syscall.h>
#include <sys/types.h> 
#include <unistd.h> 

#elif defined(__WINDOWS__)

#include <Windows.h>
#include <process.h>

#endif

#include "Thread.h"


Thread::Thread ( ) 
{
	m_TID		= 0 ;
	m_Status	= Thread::READY ;

#if defined(__WINDOWS__)
	m_hThread = NULL ;
#endif
}


////////////////////////////////////////////////////////////////////////////////
//
// destructor (virtual)
//
////////////////////////////////////////////////////////////////////////////////
Thread::~Thread () 
{
}


////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
void Thread::start () 
{ 
	if ( m_Status != Thread::READY )
		return ;

#if defined(__LINUX__)
	pthread_create( &m_TID, NULL , MyThreadProcess , this );
#elif defined(__WINDOWS__)
	m_hThread = CreateThread( NULL, 0, MyThreadProcess , this, 0, &m_TID ) ;
#endif
}


////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
void Thread::stop ()
{
}


////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
void Thread::exit( void * retval )
{
	#if defined(__LINUX__)
		pthread_exit( retval );
	#elif defined(__WINDOWS__)
		::CloseHandle( m_hThread ) ;
	#endif
}


////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
#if defined(__LINUX__)
void * MyThreadProcess ( void * derivedThread )
{
	Thread * thread = (Thread *)derivedThread;
	if( thread==NULL )
		return NULL;
	
	// set thread's status to "RUNNING"
	thread->setStatus(Thread::RUNNING);

	// here - polymorphism used. (derived::run() called.)
	thread->run();
	
	// set thread's status to "EXIT"
	thread->setStatus(Thread::EXIT);
	
	//INT ret = 0;
	//thread->exit(&ret);
	return NULL;	// avoid compiler's warning
}
#elif defined (__WINDOWS__)

DWORD WINAPI MyThreadProcess(  void* derivedThread )
{
	{
		Thread * thread = (Thread *)derivedThread;
		if( thread==NULL )
			return 0;
		
		// set thread's status to "RUNNING"
		thread->setStatus(Thread::RUNNING);

		// here - polymorphism used. (derived::run() called.)
		thread->run();
		
		// set thread's status to "EXIT"
		thread->setStatus(Thread::EXIT);

		thread->exit(NULL);
	}
	return 0;	// avoid compiler's warning
}
#endif

void Thread::run( )
{
}

tid_t GetTid()
{
#if defined(__WINDOWS__)
	return GetCurrentThreadId();
#elif defined(__LINUX__)
	return syscall(SYS_gettid);  
#endif
}

pid_t GetPid()
{
#if defined(__WINDOWS__)
	return _getpid();
#elif defined(__LINUX__)
	return getpid();
#endif
}